package com.v5.ik;

import android.graphics.Bitmap;
import android.net.Uri;
import android.support.v4.app.FragmentActivity;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;

import com.android.volley.toolbox.NetworkImageView;
import com.bumptech.glide.Glide;
import com.bumptech.glide.request.animation.GlideAnimation;
import com.bumptech.glide.request.target.SimpleTarget;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collections;
import java.util.List;

/**
 * Created by zy on 2016/7/13.
 */
public class ImgGridAdapter extends BaseAdapter {
    private FragmentActivity activity;
    private String[] imgArr;
    private List<String> imgList = new ArrayList<>();

    public ImgGridAdapter(FragmentActivity activity, String[]imgArr) {
        this.activity = activity;
        this.imgArr = imgArr;
    }

    @Override
    public int getCount() {
        return imgArr == null ? 0 : imgArr.length;
    }

    @Override
    public String getItem(int position) {
        return imgArr[position];
    }

    @Override
    public long getItemId(int position) {
        return position;
    }

    @Override
    public View getView(final int position, View convertView, ViewGroup parent) {
        ViewHolder viewHolder;
        if (convertView == null) {
            viewHolder = new ViewHolder();
            convertView = View.inflate(parent.getContext(), R.layout.img_grid_item, null);
            viewHolder.iv_grid = (ImageView) convertView.findViewById(R.id.iv_grid);
            int widthPixels = activity.getResources().getDisplayMetrics().widthPixels;
            int xy = (int) ((widthPixels-activity.getResources().getDimension(R.dimen.xy))/3);
            viewHolder.iv_grid.getLayoutParams().height = xy;
            viewHolder.iv_grid.getLayoutParams().width = xy;
            viewHolder.iv_grid.setLayoutParams(viewHolder.iv_grid.getLayoutParams());

            convertView.setTag(viewHolder);
        } else {
            viewHolder = (ViewHolder) convertView.getTag();
        }

        Log.i("qwe", "ImgGrid: " + getItem(position));
        Glide.with(parent.getContext())
                .load(getItem(position))
                .asBitmap()
                .into(viewHolder.iv_grid);
        viewHolder.iv_grid.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                imgList.clear();
                Collections.addAll(imgList, imgArr);
                ImagePreviewActivity.startPreview(activity, imgList, position);
            }
        });

        return convertView;
    }

    static class ViewHolder {
        ImageView iv_grid;
    }
}
